<?php
namespace app\admin\controller;


use app\admin\validate\AddRole;
use think\Request;
use View;
use Db;
use Session;
use app\common\model\Role as RoleModel;
use app\common\model\User as UserModel;

use app\common\controller\BaseController;


class Role extends BaseController
{


    public function __construct()
    {
        parent::__construct();

    }


    /*
     * 角色列表
     */
    public function rolelist(){
        $model = new RoleModel();
        $datas = $model->getPageList();

        foreach($datas as $key=>$item){
            $menuNames =$model->getRoleMenuById($item['id']);
            $userNames = $model->getRoleUserById($item['id']);
            $datas[$key]['menuNames'] = $menuNames;
            $datas[$key]['userNames'] = $userNames;
        }

        $this->assign('datas', $datas);
        return $this->fetch();
    }

    /*
     * 添加角色
     */
    public function addrole(){

        return $this->fetch();
    }

    /*
     * 删除角色
     */
    public function delrole(Request $request){
        $id = $request->param('id');
        $model = new RoleModel();
        try{
            if($model->delrole($id)){
                return $this->success('删除成功', url('/admin/role/rolelist'));
            }
        }catch (Exception $e){
            return $this->error($e->getMessage());
        }


        return $this->error('删除失败');

    }




    /*
     * 添加角色处理
     */
    public function addroleDo(Request $request){
        $model = new RoleModel();
        $param = $request->param();
        $validate= new AddRole();
        try{
            $result = $validate->checkDo($param);
        }catch (Exception $e){
            return $this->error($e->getMessage());
        }
        if (!$result) {
            return $this->error($validate->getError());
        }


        if($model->addrole($param)){
            return $this->success('添加成功', url('/admin/role/rolelist'));
        }

        return $this->error('添加失败');
    }


    /*
     * 角色关联菜单
     */
    public function rolemenu(Request $request){
        $model = new RoleModel();
        $role_id = $request->param('role_id');
        $roleInfo = $model->getInfoById($role_id);
        if(empty($roleInfo)){
            $this->error('角色数据不存在！');
        }

        $hasMenu = $model->getRoleMenu($role_id);

        $this->assign('roleInfo', $roleInfo);
        $this->assign('hasMenu', $hasMenu);

        return $this->fetch();
    }


    /*
     * 角色关联菜单处理
     */
    public function rolemenuDo(Request $request){
        $model = new RoleModel();
        $params = $request->param();
        $menuIds = empty($params['menu_id']) ? [] : $params['menu_id'];
        $roleId = $params['role_id'];
        $roleInfo = $model->getInfoById($roleId);
        if(empty($roleInfo)){
            $this->error('角色数据不存在！');
        }



        $model->updateRoleMenu($roleId, $menuIds);
        return $this->success('角色关联菜单成功');
    }



    /*
     * 角色关联用户
     */
    public function roleuser(Request $request){
        $userModel = new UserModel();
        $roleModel = new RoleModel();

        $role_id = $request->param('role_id');
        $roleInfo = $roleModel->getInfoById($role_id);
        if(empty($roleInfo)){
            $this->error('角色数据不存在！');
        }
        $hasUser = $roleModel->getRoleUser($role_id);
        $datas = $userModel->getPageList();

        $this->assign('roleInfo', $roleInfo);
        $this->assign('hasUser', $hasUser);
        $this->assign('datas', $datas);

        return $this->fetch();
    }


    /*
     * 保存角色用户关联
     */
    public function roleuserDo(Request $request){
        $model = new RoleModel();
        $params = $request->param();
        $userIds = empty($params['user_id']) ? [] : $params['user_id'];
        $roleId = $params['role_id'];
        $roleInfo = $model->getInfoById($roleId);
        if(empty($roleInfo)){
            $this->error('角色数据不存在！');
        }

        $model->updateRoleUser($roleId, $userIds);
        return $this->success('角色关联用户成功');
    }


}